package com.system.user.controller;

import com.system.common.utils.R;
import com.system.user.entity.WaterEntity;
import com.system.user.entityVO.LightingVO;
import com.system.user.entityVO.WaterVO;
import com.system.user.service.WaterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 账户接口
 *
 * @author HYXK
 * @email null@qq.com
 * @date 2020-03-27 11:55:11
 */
@Api(tags = "水表接口")
@RestController
@RequestMapping("/water")
public class WaterController {

    @Autowired
    private WaterService waterService;

    /**
     * 查询所有水表信息
     *
     * @return
     */
    @ApiOperation("查询所有水表信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R list() {
        List<WaterEntity> waters = waterService.selectAll();

        return R.ok().put("waters", waters);
    }

    /**
     * 模糊查询水表信息
     *
     * @param waterEntity
     * @return
     */
    @ApiOperation("模糊查询水表信息")
    @RequestMapping(value = "/findByValue", method = RequestMethod.POST)
    public R findByValue(@RequestBody WaterEntity waterEntity) {
        List<WaterEntity> waters = waterService.selectBySiteOrNumber(waterEntity);

        return R.ok().put("waters", waters);
    }

    /**
     * 新增水表信息 注：未添加判断是否存在
     *
     * @param water
     * @return
     */
    @ApiOperation("新增水表信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R add(@RequestBody WaterEntity water) {
        System.out.println(water.getId());
        boolean is_success = false;
        try {
            is_success = waterService.addWater(water);
            if (is_success) {
                return R.ok("添加成功！");
            }
            return R.error("添加失败！");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加失败！");
        }
    }

    /**
     * 修改水表信息 注：未添加判断是否存在
     *
     * @param water
     * @return
     */
    @ApiOperation("修改水表信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public R update(@RequestBody WaterEntity water) {
        boolean is_success = waterService.updateWater(water);
        if (is_success) {
            return R.ok("修改成功！");
        }
        return R.error("修改失败！");
    }

    /**
     * 删除水表信息
     *
     * @param id
     * @return
     */
    @ApiOperation("删除水表信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestParam("id") Integer id) {
        boolean is_success = waterService.delete(id);
        if (is_success) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

}
